package com.tcm.kafka.file;

import java.io.*;
import java.util.HashMap;
import java.util.*;


public class ReplaceWords {

    static Map<String,String> cnKeyValue = new HashMap();
    static Map<String,String> zn_en_map = new HashMap<>();
    static List<String> znList = new ArrayList();
    static List<String> cnList = new ArrayList();

    public static void readCnJs(){
        File cn_js = new File("E://comm-lang-zh_CN.js");

        BufferedReader br = null;
        String prevKey = null;
        try {
            br = new BufferedReader(new FileReader(cn_js));
            String content = null;
            while(null != (content = br.readLine())) {
                if (content.contains(":")) {
                    String[] strs = content.split(":");
                    if (strs[1].trim().startsWith("{")) {
                        prevKey = strs[0].trim();
                    } else if (strs[1].trim().startsWith("\"") || strs[1].trim().startsWith("'")) {
                        String key = strs[0].trim();
                        String value = strs[1].trim().replaceAll("\"", "").replace("'", "").replaceAll(",","").replaceAll("，","");
                        cnKeyValue.put(prevKey + "." + key, value);
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(null != br){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void readTxt(File file, String type){
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(file));
            String content = null;
            while(null != (content = br.readLine())){
                if(type.equals("zn")) {
                    znList.add(content.trim());
                }else{
                    cnList.add(content.trim());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(null!=br){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void readWriteEnjs(){
        File en_js = new File("E://comm-lang-en.js");
        File new_en_js = new File("E://en.js");
        BufferedReader br = null;
        BufferedWriter bw = null;
        String prevKey = null;

        try {
            br = new BufferedReader(new FileReader(en_js));
            bw = new BufferedWriter(new FileWriter(new_en_js));

            String content = null;
            while(null != (content = br.readLine())){
                if(content.contains(":")){
                    String[] strs = content.split(":");
                    if (strs[1].trim().startsWith("{")) {
                        prevKey = strs[0].trim();
                    } else if (strs[1].trim().startsWith("\"") || strs[1].trim().startsWith("'")) {
                        String key = strs[0].trim();
                        String znValue = cnKeyValue.get(prevKey+"."+key);
                        if(znValue == null){
                            System.out.println("【未查到中文】key："+prevKey+"."+key+","+"value:"+strs[1]);
                            bw.write(content+"\r\n");
                            continue;
                        }
                        String enValue = zn_en_map.get(znValue);
                        if(enValue == null){
                            System.out.println("【未查到英文】key："+prevKey+"."+key+","+"value:"+znValue);
                            bw.write(content+"\r\n");
                            continue;
                        }
//                        String oldVal = strs[1].trim().replaceAll("\"","").replaceAll("\'","").replaceAll(",","").replaceAll("，","");
//                        content = content.replace(oldVal, enValue);
                        String subStr = content.substring(0, content.indexOf(":"));
                        content = subStr + ":\"" + enValue + "\",";
                    }
                }
                bw.write(content+"\r\n");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(null != bw){
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(null != br){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {

        readCnJs();
        File znTxt = new File("E://zn.txt");
        readTxt(znTxt, "zn");
        File cnTxt = new File("E://cn.txt");
        readTxt(cnTxt, "cn");

        for(int i=0;i<znList.size();i++){
            zn_en_map.put(znList.get(i), cnList.get(i));
        }
        readWriteEnjs();
    }

}
